package org.example.demo.service;

import org.example.demo.enums.BookStatus;
import org.example.demo.mapper.BookMapper;
import org.example.demo.model.BookInfo;
import org.example.demo.model.PageRequest;
import org.example.demo.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service  //告诉Spring 帮我们存对象
public class BookService {
    @Autowired
    private BookMapper bookMapper;


    public Integer insertBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    //
    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
        //1.获取总记录数
        Integer count = bookMapper.count();
        //2. 获取当前页的记录
        List<BookInfo> bookInfos = bookMapper.queryBookBypage(pageRequest.getOffset(), pageRequest.getPageSize());//用Page里的参数执行Mapper里的Sql语句
        //3. 处理状态
        //4. 状态  0-删除  1-可借阅  2-不可借阅
        for (BookInfo bookInfo: bookInfos) {  //业务逻辑层
            bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResult(bookInfos,count,pageRequest);  //前端接收
    }

    //根据Id查看图书信息
    public BookInfo queryBookById(Integer bookId) {

        BookInfo bookInfo = bookMapper.queryBookById(bookId);
        bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        return bookInfo;
    }

    public Integer updateBookById(BookInfo bookInfo) {
        return bookMapper.updateBookById(bookInfo);
    }


/*    public Integer deleteBook(BookInfo bookInfo) {  //这个也一样是复用update的sql语句
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(0);

        return bookMapper.updateBookById(bookInfo);
    }*/

    //
    public Integer batchDeleteBookByIds(List<Integer> ids) {
        return bookMapper.batchDeleteBookByIds(ids);
    }


}
